/*
 * sht20.c
 *
 *  Created on: May 13, 2023
 *      Author: zhulijun
 */

#include "my_inc.h"

/*
#ifdef CONFIG_SHT20_DEBUG
#define sht20_print(format, args...)  printf(format, ##args)
#else
#define sht20_print(format, args...)  do{}  while(0)
#endif
*/

#include "my_inc.h"

/*========================================
	函数名称：SHT20_Init
	函数功能：SHT20 初始化操作
	说    明：SCL(PF13) SDA(PF14)
=========================================*/

void SHT20_Init()
{
	IIC_Init();
}


/*========================================
	函数名称：SHT20_ReadData
	函数功能：给某个地址写入数据
=========================================*/
uint16_t SHT20_ReadData(uint8_t ADDR)
{
	uint16_t data = 0;

	//发送起始信号
	IIC_Start();

	//发送 地址 +写
	IIC_SendByte(SHT20_W_ADDR);
	if (IIC_WaitAck() != ACK)
	{
		IIC_Stop();
		return 1;
	}

	//发送命令
	IIC_SendByte(ADDR);    //让设备进行采样温度或者湿度
	if (IIC_WaitAck() != ACK)
	{
		IIC_Stop();
		return 1;
	}
	HAL_Delay(25);

	while (1)
	{
		IIC_Start();
		IIC_SendByte(SHT20_R_ADDR);
		if (IIC_WaitAck() != ACK)
		{
			IIC_Stop();
			continue;
		}
		else
			break;
	}

	data |= IIC_ReadByte(ACK) << 8;
	data |= IIC_ReadByte(NOACK);

	IIC_Stop();

	//printf("sht20_d:%d",data);

	return data;
}

double SHT20_Get_T_Value(uint16_t data)
{

	return (-46.85 + 175.72 * data / 65536);
}

double SHT20_Get_RH_Value(uint16_t data)
{
	return (-6 + 125 * data / 65536);
}







